package com.sphd.restaurantsystem.activitys.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.info.TopUpInfo;
import com.sphd.restaurantsystem.presenter.CzPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.CzView;
import com.wzh.frame.utils.MyUtils;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddVipActivity extends BaseActivity implements CzView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.vip_num)
    MaterialEditText vipNum;
    @BindView(R.id.vip_name)
    MaterialEditText vipName;
    @BindView(R.id.vip_phone)
    MaterialEditText vipPhone;
    @BindView(R.id.viptype_textview)
    TextView viptypeTextview;
    @BindView(R.id.add_vip_type_spinner)
    Spinner addVipTypeSpinner;
    @BindView(R.id.vip_money_spinner)
    Spinner vipMoneySpinner;
    @BindView(R.id.update)
    Button update;
    @BindView(R.id.content_add_vip)
    NestedScrollView contentAddVip;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    CzPresenterImpl czPresenter;
    List<String> strings;
    ArrayAdapter arrayAdapter;
    List<TopUpInfo> topUpInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_vip);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());

        addVipTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 2) {
                    vipMoneySpinner.setVisibility(View.VISIBLE);
                } else {
                    vipMoneySpinner.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setTitleBar(toolbar,"添加会员");
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        czPresenter = new CzPresenterImpl(this);
        topUpInfos = new ArrayList<>();
        strings = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
        vipMoneySpinner.setAdapter(arrayAdapter);

        czPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.CzList + "cgid=" + restaurantInfo.getId());

    }

    @Override
    public void recList(List<TopUpInfo> topUpInfos) {
        this.topUpInfos.clear();
        this.topUpInfos = topUpInfos;
        strings.clear();
        for (TopUpInfo topUpInfo : topUpInfos) {
            strings.add("缴费" + topUpInfo.getReceipt_money() + "充值" +
                    topUpInfo.getAdd_money());
        }
        arrayAdapter.notifyDataSetChanged();
    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("添加成功！");
            bundle.putInt(Constant.TYPE,addVipTypeSpinner.getSelectedItemPosition());
            intent.putExtras(bundle);
            setResult(2,intent);
            finish();
        }else {
            toast("手机号码或会员卡号重复！");
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dissLoading() {

    }

    @OnClick(R.id.update)
    public void onClick() {
        if (!TextUtils.isEmpty(vipNum.getText().toString().trim())) {
            if (vipNum.getText().toString().trim().equals(vipPhone.getText().toString().trim())) {
                Toast.makeText(context, "卡号和手机号一样无法添加", Toast.LENGTH_SHORT).show();
                return;
            } else if (MyUtils.isMobileNO(vipPhone.getText().toString().trim())) {
                if (vipNum.getMaxCharacters() <= 15) {
                    czPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.AddVip+
                    "cgid="+restaurantInfo.getId()+"&cgname="+ restaurantInfo.getCg_name()+"&memname="+
                            StringUtils.utf8Encode(vipName.getText().toString().trim())+"&card="+vipNum.getText().toString().trim()+"&memphone="+
                            vipPhone.getText().toString().trim()+"&status="+addVipTypeSpinner.getSelectedItemPosition()+
                            (addVipTypeSpinner.getSelectedItemPosition()==2?(
                                    "&price="+topUpInfos.get(vipMoneySpinner.getSelectedItemPosition()).getReceipt_money()+
                            "&money="+topUpInfos.get(vipMoneySpinner.getSelectedItemPosition()).getAdd_money()):""));
                } else {
                    Toast.makeText(context, "卡号过长", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(context, "输入正确手机号", Toast.LENGTH_SHORT).show();
                return;
            }

        } else {
            Toast.makeText(context, "卡号为空", Toast.LENGTH_SHORT).show();
            return;
        }
    }
}
